﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ServiceStack.Text;

namespace PersonaLibrary.ViewModel.AlphabetViewModel
{
	internal class AlphabetViewModel : BaseViewModel
	{
		private char _selectedChar;
		private bool _isEnable;
		private bool _registry;
		private string _selectedProperty;
		private readonly string _alphabet = "абвгдеёжзийклмнопрстуфхцчшщыэюя".ToUpper();
		private readonly string _filteringType;
		private readonly SerializeProperties _properties;
		private readonly List<Metadata.Point> _availableProperties;

		public AlphabetViewModel(string filteringType)
		{
			string appDataFolder = string.Format(@"{0}\Personalii\AlphabetFilter", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData,
												Environment.SpecialFolderOption.DoNotVerify));

			DirectoryInfo info = new DirectoryInfo(appDataFolder);
			if(!info.Exists)
				info.Create();
			FileStream file = new FileStream(string.Format(@"{0}\{1}.json", appDataFolder, filteringType), FileMode.OpenOrCreate);

			_properties = JsonSerializer.DeserializeFromStream<SerializeProperties>(file);

			file.Close();

			_filteringType = filteringType;

			_availableProperties = Metadata.Instance[filteringType].Where(x => x.IsFilter).ToList();

			if (_properties != null)
			{
				_selectedChar = _properties.SelectedChar;
				_isEnable = _properties.IsEnable;
				_registry = _properties.Registry;
				_selectedProperty = _properties.SelectedProperty;
			}
			else
			{
				_properties = new SerializeProperties();
				SelectedProperty = "Name";
				SelectedChar = 'А';
				Registry = false;
				IsEnable = true;
			}
		}

		public char SelectedChar
		{
			get { return _selectedChar; }
			set
			{
				_selectedChar = value;
				_properties.SelectedChar = value;
				RisePropertyChanged("SelectedChar");
			}
		}

		public bool IsEnable
		{
			get { return _isEnable; }
			set
			{
				_isEnable = value;
				_properties.IsEnable = value;
				RisePropertyChanged("IsEnable");
			}
		}

		public bool Registry
		{
			get { return _registry; }
			set
			{
				_registry = value;
				_properties.Registry = value;
				RisePropertyChanged("Registry");
				RisePropertyChanged("Alphabet");
			}
		}

		public string SelectedProperty
		{
			get { return _selectedProperty; }
			set
			{
				_selectedProperty = value;
				_properties.SelectedProperty = SelectedProperty;
				RisePropertyChanged("SelectedProperty");
			}
		}

		public char[] Alphabet
		{
			get
			{
				if (!Registry)
					return _alphabet.ToArray();
				
				return string.Concat(_alphabet, _alphabet.ToLower()).ToArray();
			}
		}

		public List<Metadata.Point> AvailableProperties
		{
			get { return _availableProperties; }
		}

		protected override void DelayPropertyChanged()
		{
			string appDataFolder = string.Format(@"{0}\Personalii\AlphabetFilter", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData,
												Environment.SpecialFolderOption.Create));

			FileStream file = new FileStream(string.Format(@"{0}\{1}.json", appDataFolder, _filteringType), FileMode.OpenOrCreate);

			JsonSerializer.SerializeToStream(_properties, file);

			file.Close();

			base.DelayPropertyChanged();
		}

		private class SerializeProperties
		{
			public char SelectedChar { set; get; }

			public bool IsEnable { set; get; }

			public bool Registry { set; get; }

			public string SelectedProperty { set; get; }
		}
	}
}
